<?php

namespace app\model;

use think\facade\Cache;
use think\Model;

class RedisModel {


    /*
   * get方法
   * */
    public function has($key){
        if(empty($key)){
            return null;
        }

        return Cache::store('redis')->has($key);
    }

    /*
     * get方法
     * */
    public function get($key){
        if(empty($key)){
            return null;
        }

        return Cache::store('redis')->get($key);
    }


    /*
     * set 方法
     * */
    public function set($key,$value,$expire=0){
        if(empty($key)){
            return null;
        }
        return  Cache::store('redis')->set($key,$value,$expire);
    }


    /*
    * 自增
    * */
    public function incr($key,$step = 1){
        if(empty($key)){
            return null;
        }
        return    Cache::store('redis')->inc($key,$step);
    }


    /*
   * 自减
   * */
    public function decr($key,$step = 1){
        if(empty($key)){
            return null;
        }
        return    Cache::store('redis')->dec($key,$step);
    }



    /*
   * 删除
   * */
    public function delete($key){
        if(empty($key)){
            return null;
        }
        return Cache::store('redis')->delete($key);
    }



    /*
  * 删除
  * */
    public function deleteAll($key){
        if(empty($key)){
            return null;
        }

        $prefix = config("cache.stores.redis.prefix");
        $key = $prefix . $key."*";
        $keys = Cache::store('redis')->keys($key);
        foreach ($keys as $k){
            $k = str_replace($prefix,"",$k);
            $this->delete($k);
        }

        return true;
    }




}